import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import {fileURLToPath} from 'url'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {
    ElementPlusResolver
} from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'

const __filenameNew = fileURLToPath(import.meta.url)
const __dirnameNew = path.dirname(__filenameNew)


const viteConfig = defineConfig(({command, mode}) => {
    const env = loadEnv(mode, process.cwd() + "\\env", '');
    return {
        base: env.ENV_BASE_PATH,
        build: {
            outDir: "../admin/src/main/resources/static/admin",
            chunkSizeWarningLimit: 1024 * 1024 * 5,
        },
        // 解析
        resolve: {
            // 别名
            alias: {
                "@": path.resolve(__dirnameNew, "src"),
            },
        },

        optimizeDeps: {
            // 将指定数组中的依赖不进行依赖预构建
            exclude: []
        },
        // 环境的默认目录
        envDir: "env",
        // 把默认的 VITE_ 换成我们自定义的前缀
        envPrefix: "ENV_",
        // 设置代理
        server: {
            host: '0.0.0.0',
            port: 3000,
            proxy: {
                '/api': {
                    target: env.ENV_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                },
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@import "@/assets/style/main.scss";'
                }
            }
        },
        plugins: [
            vue(),
            Components({
                resolvers: [
                    ElementPlusResolver(),
                    IconsResolver()
                ]
            }),
            AutoImport({
                // 注册全局导入
                imports: [
                    'vue',
                    'vue-router',
                    'pinia',
                    'vue-i18n',
                    {
                        '@vueuse/core': [
                            // import { useMouse } from '@vueuse/core',
                            'useMouse',
                            // import { useFetch as useMyFetch } from '@vueuse/core',
                            // alias
                            ['useFetch', 'useMyFetch'],
                        ],
                        'axios': [
                            // import { default as axios } from 'axios',
                            // default imports
                            ['default', 'axios'],
                        ]
                    },
                ],
                dirs: [
                    '@/components'
                ],
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icon/svg')],
                symbolId: '[name]'
            }),
            Icons({
                compiler: 'vue3',
            }),
        ]
    }
})


export default viteConfig;